'use strict';

const path = require('path');
const fileSrv = require('../utility/fileSrv');
const dataConst = require('../constant/data-const');

const Service = require('egg').Service;

class ImageService extends Service {
    async updateMeetingImageSrv(meetingId, resDir, imageName) {
        const { app } = this;

        const meetingResult = await app.model.Meeting.findOne({
            where: {
                id: meetingId
            }
        });

        if (meetingResult) {
            if (meetingResult.image) {
                fileSrv.deleteFile(path.join(resDir, meetingResult.image));
            }
            meetingResult.image = imageName;
            return await meetingResult.save();
        } else {
            fileSrv.deleteFile(path.join(resDir, imageName));
            return false;
        }
    }

    async updateUserAvatarSrv(userId, resDir, imageName) {
        const { app } = this;

        const userResult = await app.model.User.findOne({
            where: {
                id: userId
            }
        });

        if (userResult) {
            if (userResult.avatar) {
                fileSrv.deleteFile(path.join(resDir, userResult.avatar));
            }
            userResult.avatar = imageName;
            return await userResult.save();
        } else {
            fileSrv.deleteFile(path.join(resDir, imageName));
            return false;
        }
    }

    async addMemberPhotoSrv(userId, resDir, imageName) {
        const { app } = this;

        const result = await app.model.UserPhoto.create({
            user_id: userId,
            image: imageName,
            type: dataConst.imageType.photo.value
        });

        if (!result) {
            fileSrv.deleteFile(path.join(resDir, imageName));
            return false;
        }

        return result;
    }

    async getMemberPhotoListSrv(userId) {
        const { ctx } = this;

        const queryString =
            `select
                image as photo
                , concat(:serverUrl, :photoPath, :userId, '/', image) as photo_url
                from tbl_user_photo
                where true
                and user_id = :userId
                and type = :type
                order by created_at asc`;

        const paramsData = {
            serverUrl: dataConst.serverUrl,
            photoPath: dataConst.photoPath,
            userId,
            type: dataConst.imageType.photo.value
        };
        return await ctx.helper.sqlQuery(queryString, paramsData);
    }

    async removeMemberPhotoSrv(userId, imageName) {
        const { app } = this;

        const result = await app.model.UserPhoto.destroy({
            where: {
                user_id: userId,
                image: imageName,
                type: dataConst.imageType.photo.value
            }
        });

        const resourceDir = path.join(app.resDir, 'member', `photo${userId}`);
        fileSrv.deleteFile(path.join(resourceDir, imageName));

        return result;
    }

    async getMemberIdentitySrv(userId) {
        const { ctx } = this;

        const queryString =
            `select
                type, image
                , concat(:serverUrl, :identityPath, :userId, '/', image) as image_url
                from tbl_user_photo
                where true
                and user_id = :userId
                and type in (:idUp, :idDown)
                order by type asc`;

        const paramsData = {
            serverUrl: dataConst.serverUrl,
            identityPath: dataConst.identityPath,
            userId,
            idUp: dataConst.imageType.idUp.value,
            idDown: dataConst.imageType.idDown.value
        };
        return await ctx.helper.sqlQuery(queryString, paramsData);
    }

    async addMemberIdentitySrv(userId, imageType, resDir, imageName) {
        const { app } = this;

        const result = await app.model.UserPhoto.create({
            user_id: userId,
            image: imageName,
            type: imageType
        });

        if (!result) {
            fileSrv.deleteFile(path.join(resDir, imageName));
            return false;
        }

        return result;
    }

    async removeMemberIdentitySrv(userId, imageType, imageName) {
        const { app } = this;

        const result = await app.model.UserPhoto.destroy({
            where: {
                user_id: userId,
                image: imageName,
                type: imageType
            }
        });

        const resourceDir = path.join(app.resDir, 'member', `identity${userId}`);
        fileSrv.deleteFile(path.join(resourceDir, imageName));

        return result;
    }
}

module.exports = ImageService;
